//
//  LWQSearchView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/22.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "LWQSearchView.h"

@implementation LWQSearchView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame initWithType:(SearchViewType)type andImageName:(NSString *)imageName{
    if ([super initWithFrame:frame]) {
        self.type = type;
        self.imageName = imageName;
        [self initSubView];
        [self layout];
    }
    return self;
}
- (void)initSubView{
    [self addSubview:self.backView];
    [_backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0);
        make.bottom.equalTo(@0);
        make.left.equalTo(@0);
        make.right.equalTo(@0);
    }];
    [_backView addSubview:self.searchTF];
    [_searchTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@15);
        make.right.equalTo(self->_backView.mas_right).offset(-15);
        make.top.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
    

}

- (void)layout{
    
}
- (UIView *)backView{
    if (!_backView) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = MAINBACKCOLOR;
        view.layer.cornerRadius = 15;
        view.layer.masksToBounds = YES;
        _backView = view;
    }
    return _backView;
}
- (UITextField *)searchTF{
    if (!_searchTF) {
        UITextField *tf = [[UITextField alloc]init];
        tf.backgroundColor = MAINBACKCOLOR;
        tf.placeholder = @" 搜索";
        tf.textColor = TITLECOLOR;
        tf.delegate = self;
        tf.font = [UIFont systemFontOfSize:14];
        if (_type == 0) {
            [self setTextFiledLeftImage:tf image:self.imageName];
        }else if (_type == 1){
            [self setTextFiledRightImage:tf image:self.imageName];
        }else{
            [self setTextFiledCenterImage:tf image:self.imageName];
        }
        
        _searchTF = tf;
    }
    return _searchTF;
}
-(void)setTextFiledLeftImage:(UITextField*)textFiled image:(NSString*)image{
    textFiled.leftViewMode = UITextFieldViewModeAlways;
    UIImageView *paddingView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 0, 16, 16)];
    paddingView.image = [UIImage imageNamed:image];
    textFiled.leftView = paddingView;
}
-(void)setTextFiledRightImage:(UITextField*)textFiled image:(NSString*)image{
    textFiled.rightViewMode = UITextFieldViewModeAlways;
    UIImageView *paddingView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 0, 16, 16)];
    paddingView.image = [UIImage imageNamed:image];
    textFiled.rightView = paddingView;
}
-(void)setTextFiledCenterImage:(UITextField*)textFiled image:(NSString*)image{
//    textFiled. = UITextFieldViewModeAlways;
    textFiled.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    UIImageView *paddingView = [[UIImageView alloc] initWithFrame:CGRectMake(textFiled.frame.size.width/2-30, 0, 16, 16)];
    paddingView.image = [UIImage imageNamed:image];
    textFiled.rightView = paddingView;
}
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    [_searchTF resignFirstResponder];
    if (self.block) {
        self.block(theTextField.text);
    }
    
    NSDictionary *dict = @{@"keyString":theTextField.text};
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"keyString" object:self userInfo:dict];
    
    return YES;
}
@end
